home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ev_201.zip / HANOI.ZIP / TTOWER.CPP < prev    next >
C/C++ Source or Header  |  1993-07-03  |  6KB  |  160 lines

  1. /*
  2.    Module        : TTOWER.CPP
  3.    Version       : 2.0
  4.    Revision date : July 3rd, 1993
  5.    Author(s)     : Remy Gendron
  6.  
  7.    Description   : ttower member functions.
  8. */
  9.  
  10.  
  11. // Headers ------------------------------------------------------------------
  12.  
  13. #include <conio.h>                                      // System's libraries
  14. #include <stdio.h>
  15. #pragma hdrstop
  16.  
  17. #include "main.hpp"                                          // Other modules
  18. #include "stdfcts.h"
  19. #include "twindow.hpp"
  20.  
  21. #include "ttower.hpp"                            // This module's header file
  22.  
  23.  
  24. // --------------------------------------------------------------------------
  25.  
  26. ttower::ttower                                                 // Constructor
  27. (
  28.    int           tid,                                           // Tower's ID
  29.    int           trow,               // Tower's bottom left corner's position
  30.    int           tcol,
  31.    int           tcolor,                                     // Tower's color
  32.    twindow huge *twinptr                                // Ptr to work window
  33. )
  34.  
  35. {
  36.    int loop ;                                                      // Counter
  37.  
  38.  
  39.    id = tid ;                                    // Initialises tower's infos
  40.    row = trow ;
  41.    col = tcol ;
  42.    color = tcolor ;
  43.    nb_disks = 0 ;
  44.    for (loop=0 ; loop<=9 ; loop++)
  45.       content[loop] = NULL ;
  46.    winptr = twinptr ;
  47.  
  48.  
  49.    return ;                                     // Tower has been initialised
  50. }                                                          // End constructor
  51.  
  52.  
  53. // --------------------------------------------------------------------------
  54.  
  55. ttower::~ttower ()                                              // Destructor
  56.  
  57. {
  58.    return ;                                                  // Nothing to do
  59. }                                                           // End Destructor
  60.  
  61.  
  62. // --------------------------------------------------------------------------
  63.  
  64. void ttower::show ()                                  // Displays empty tower
  65.  
  66. {
  67.    int  loop ;                                                     // Counter
  68.  
  69.  
  70.    for (loop=0 ; loop<11 ; loop++)                             // Draws shaft
  71.       winptr->winwrite ("░█",row-12+loop,col+10,0,color) ;
  72.  
  73.    for (loop=0 ; loop<2 ; loop++)                               // Draws base
  74.       winptr->winwrite ("░░░▒▒▒▒▓▓▓▓▓████▓▓▓▒▒░",row-1+loop,col,0,color) ;
  75.  
  76.  
  77.    return ;                                       // Tower has been displayed
  78. }                                                         // End ttower::show
  79.  
  80.  
  81. // --------------------------------------------------------------------------
  82.  
  83. int ttower::getrow ()               // Returns tower's bottom left corner row
  84.  
  85. {
  86.    return row ;                     // Tower's row position has been returned
  87. }                                                       // End ttower::getrow
  88.  
  89.  
  90. // --------------------------------------------------------------------------
  91.  
  92. int ttower::getcol ()               // Returns tower's bottom left corner col
  93.  
  94. {
  95.    return col ;                     // Tower's col position has been returned
  96. }                                                       // End ttower::getcol
  97.  
  98.  
  99. // --------------------------------------------------------------------------
  100.  
  101. int ttower::getcolor ()                              // Returns tower's color
  102.  
  103. {
  104.    return color ;                          // Tower's color has been returned
  105. }                                                     // End ttower::getcolor
  106.  
  107.  
  108. // --------------------------------------------------------------------------
  109.  
  110. int ttower::getnbdisks ()             // Returns nb of disks stacked on tower
  111.  
  112. {
  113.    return nb_disks ;            // Number of disks on tower has been returned
  114. }                                                   // End ttower::getnbdisks
  115.  
  116.  
  117. // --------------------------------------------------------------------------
  118.  
  119. void ttower::pushdisk                                 // Adds a disk to tower
  120. (
  121.    tdisk huge *disk                                     // Ptr to disk pushed
  122. )
  123.  
  124. {
  125.    assert (nb_disks<10,"ttower::pushdisk","Tower full",1) ;    // Tower full?
  126.  
  127.    content[nb_disks] = disk ;                                     // One more
  128.    nb_disks++ ;
  129.  
  130.  
  131.    return ;                                    // One disk added to the tower
  132. }                                                     // End ttower::pushdisk
  133.  
  134.  
  135. // --------------------------------------------------------------------------
  136.  
  137. void ttower::popdiskto                       // Moves a disk to another tower
  138. (
  139.    ttower huge *target,                           // Ptr to destination tower
  140.    int         speed                                     // Animation's speed
  141. )
  142.  
  143. {
  144.    assert (nb_disks > 0,"ttower::popdisk","Tower empty",1) ;  // Tower empty?
  145.  
  146.    content[nb_disks-1]->moveto (target->getrow ()- 2 - target->getnbdisks (),
  147.                                 target->getcol (),
  148.                                 color,target->getcolor (),speed) ;
  149.  
  150.    target->pushdisk (content[nb_disks-1]) ;     // Moves disks to other tower
  151.    content[nb_disks-1] = NULL ;                              // One less here
  152.    nb_disks-- ;
  153.  
  154.  
  155.    return ;                             // Disk has been moved to other tower
  156. }                                                      // End ttower::popdisk
  157.  
  158.  
  159. // End source file ----------------------------------------------------------
  160.